package nlppatents.apps;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import nlppatents.util.DictWord;
import nlppatents.util.POS;
import nlppatents.util.Patent;
import nlppatents.util.PatentTxtDirectoryScanner;
import nlppatents.util.Phrase;
import nlppatents.util.Sentence;
import nlppatents.util.WordCategory;

/**
 * Reads all files stored in the "txt" directory, processes them,
 * and 
 * @author bresee
 *
 */
public class Semlex 
{
	public static ArrayList<WordCategory> categories;
	public static PatentTxtDirectoryScanner scanner;
	
	public static void LoadSeeds(String filename)
	{
		try 
		{
			BufferedReader input = new BufferedReader(new FileReader(filename));
			WordCategory W = null;
			
			try 
			{
				String line = null; //not declared within while loop
				
				while ((line = input.readLine()) != null) 
				{
					if(line.equals("category:"))
					{
						W = new WordCategory();
						categories.add(W);
					}
					else
					{
						DictWord D = DictWord.get(line.trim());
						W.members.add(D);
						W.colocVals.put(D, new Integer(0));
					}
				}
			} 
			finally {
				input.close();
			}
		} catch (IOException ex) {
			ex.printStackTrace();
			System.exit(1);
		}
		
	}
	
	public static final int ITERATIONS = 8;
	public static final int TAKE = 3;
	
	public static void main(String args[])
	{
		scanner = new PatentTxtDirectoryScanner(7000000, 7009999);
		scanner.incFrequency = true; // first run, turn this on
		
		LoadSeeds("../../seeds.txt");
	}
	
	
	public static void processPatent(Patent patent)
	{
		// do title
		processSentence(patent.title);
		
		int size = 0;
		
		// do abstract
		size = patent.pAbstract.size();
		for(int i = 0; i < size; i++)
		{
			processSentence(patent.pAbstract.get(i));
		}
		
		// do claims
		size = patent.pClaims.size();
		for(int i = 0; i < size; i++)
		{
			processSentence(patent.pClaims.get(i));
		}
		
		// do description
		size = patent.pDescription.size();
		for(int i = 0; i < size; i++)
		{
			processSentence(patent.pDescription.get(i));
		}
	}
	
	public static void processSentence(Sentence sentence)
	{
		int size = sentence.sentence.size();
		
		for(int i = 0; i < size; i++)
		{
			Phrase p = sentence.sentence.get(i);
			// see if this is a phrase is full-o nouns
			if((p.type == POS.noun) || (p.type == POS.prep))
			{
				// it is!  see if it has any colocated friends
				DictWord col = colocForward(sentence, i);
				if(col != null) // friends found!  add DictWords to each others lists
				{
					// get the head noun of the colocated phrase
					DictWord head = p.words.get(p.words.size()-1).dictWord;
					if(col != head)
					{
						Integer headI = ;
						
						// increase collocation values
						head.coloc++;
						col.coloc++;
					}
				}
			}
		}
	}
	
	/**
	 * Determines whether phrase sentence.sentence.get(index) is colocated with a following
	 * phrase.
	 * 
	 * @param sentence
	 * @param index
	 * @return
	 */
	public static DictWord colocForward(Sentence sentence, int index)
	{
		// pointless to check if we are 2nd from last (or greater) term because there
		// will be no room for a joining term (and, or, comma)
		if(index >= sentence.size() - 2)
			return null;
		
		index++;
		Phrase nextP = sentence.get(index);
		boolean found = false;
		
		/* yes, I realize if we had a crazy weird sentence like:
		 * 	 "NP , and , or , and , , and or NP" 
		 * this would be valid.  I will fix it later
		 * 
		 * TODO:  Make this more valid
		 */
		while((nextP.type == POS.cc) || (nextP.type == POS.comma))
		{
			found = true;
			index++;
			if(!(index < sentence.size()))
				return null;
			nextP = sentence.get(index);
		}
		
		if(!found || ((nextP.type != POS.noun) && (nextP.type != POS.prep)))
			return null;
		
		return nextP.words.get(nextP.words.size() - 1).dictWord;
	}
}
